﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.Practices.ServiceLocation;
using TFS.Kanban.Commons;
using TFS.Kanban.Domain;
using TFS.Kanban.Domain.Services;
using TFS.Kanban.Modules.KanbanBoard.Interfaces;

namespace TFS.Kanban.Modules.KanbanBoard.ViewModels
{
    public class LaneViewModel : ViewModelBase<LaneViewModel>, ILaneViewModel
    {
        private readonly IUserStoryService _userStoryService;

        public LaneViewModel(IUserStoryService userStoryService)
        {
            _userStoryService = userStoryService;
        }

        #region Properties

        private string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                if (_Name != value)
                {
                    _Name = value;
                    OnPropertyChanged(x => x.Name);
                }
            }
        }

        private ICollection<ICardViewModel> _Cards;
        public ICollection<ICardViewModel> Cards
        {
            get { return _Cards; }
            private set
            {
                if (_Cards != value)
                {
                    _Cards = value;
                    OnPropertyChanged(x => x.Cards);
                }
            }
        }

        #endregion

        #region CRUD

        public void Load(string name, IEnumerable<IUserStory> cards)
        {
            this.Name = name;

            LoadCards(cards);
        }

        private void LoadCards(IEnumerable<IUserStory> cardsToLoad)
        {
            this.Cards = new ObservableCollection<ICardViewModel>();

            foreach (var cardToLoad in cardsToLoad)
            {
                var cardViewModel = ServiceLocator.Current.GetInstance<ICardViewModel>();
                cardViewModel.Load(cardToLoad);
                this.Cards.Add(cardViewModel);
            }
        }

        #endregion
    }
}
